// 文件名：AliPayServiceImpl.java
package com.example.payment.service.impl;

import com.alipay.api.*;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.example.core.exception.HotelException;
import com.example.payment.config.AliPayConfig;
import com.example.payment.service.AliPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.example.model.constants.CodeConstants.ALI_ERROR;

@Service
public class AliPayServiceImpl implements AliPayService {
    @Autowired
    private AliPayConfig aliPayConfig;


    /**
     * 支付宝沙箱支付
     * @param outTradeNo
     * @param totalAmount
     * @param subject
     * @return
     * @throws AlipayApiException
     */
    @Override
    public AlipayTradePrecreateResponse createPreOrder(String outTradeNo, String totalAmount, String subject) throws AlipayApiException {
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl(aliPayConfig.getNotifyUrl());
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
        // 设置基础参数
        model.setOutTradeNo(outTradeNo);
        model.setTotalAmount(totalAmount);
        model.setSubject(subject);
        model.setBody(subject);

        request.setBizModel(model);

        //处理支付宝接口调用异常
        AlipayTradePrecreateResponse response;
        try {
            response = aliPayConfig.alipayClient().execute(request);
        } catch (AlipayApiException e) {
            throw new HotelException(ALI_ERROR ,"调用支付宝接口发生异常");
        }

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
//            System.out.println("调用失败");
            throw new HotelException(ALI_ERROR ,"调用支付宝接口失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
        return response;
    }

    /**
     * 支付宝沙箱退款
     * @param outTradeNo
     * @param refundAmount
     * @return
     * @throws AlipayApiException
     */
    @Override
    public AlipayTradeRefundResponse AlipayTradeRefund(Long outTradeNo, String refundAmount) throws AlipayApiException{

        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
//      System.out.println(aliPayConfig.getRefundNotifyUrl());
        request.setNotifyUrl(aliPayConfig.getRefundNotifyUrl());
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();
        // 设置基础参数
        model.setOutTradeNo(outTradeNo.toString());//设置订单号
        model.setRefundAmount(refundAmount);//设置退款金额
        model.setOutRequestNo("HZ01RF001");   // 设置退款请求号
        request.setBizModel(model);
        //处理支付宝接口调用异常
        AlipayTradeRefundResponse response;
        try {
            response = aliPayConfig.alipayClient().execute(request);
        } catch (AlipayApiException e) {
            System.out.println("调用支付宝接口发生异常: " + e.getMessage());
            throw new HotelException(ALI_ERROR ,"调用支付宝接口发生异常");
        }

        if (response.isSuccess()) {
            System.out.println("调用成功");
//            System.out.println(request.getNotifyUrl() );
//            System.out.println(response.getBody());
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
        return response;
    }

}


